package com.base.pig.controller;


import com.alibaba.nacos.shaded.io.grpc.netty.shaded.io.netty.util.internal.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.pig.entity.ScSensorInfo;
import com.base.pig.service.ScSensorInfoService;
import com.base.pig.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjh
 * @since 2022-04-19
 */
@RestController
@Api(value = "monitor", tags = "传感器")
@RequestMapping("/sensor")
public class ScSensorInfoController {

    @Autowired
    public ScSensorInfoService scSensorInfoService;

    @ApiOperation(value = "条件查询")
    @PostMapping("/list")
    public R list(@RequestBody ScSensorInfo scSensorInfo){

        //属性为null或者"" ,查询全部
        QueryWrapper<ScSensorInfo> wrapper = new QueryWrapper<ScSensorInfo>();

        wrapper.eq(!StringUtil.isNullOrEmpty(String.valueOf(scSensorInfo.getId()) )
                &&  !"null".equals(String.valueOf(scSensorInfo.getId())), "id",scSensorInfo.getId());
        wrapper.eq(!StringUtil.isNullOrEmpty(scSensorInfo.getSensorName()), "sensor_name",scSensorInfo.getSensorName());
        wrapper.eq(!StringUtil.isNullOrEmpty(scSensorInfo.getSensorUnit()), "sensor_unit",scSensorInfo.getSensorUnit());
        wrapper.eq(!StringUtil.isNullOrEmpty(scSensorInfo.getSensorMetric()), "sensor_metric",scSensorInfo.getSensorMetric());
        wrapper.eq(!StringUtil.isNullOrEmpty(scSensorInfo.getSensorCode()), "sensor_code",scSensorInfo.getSensorCode());

        List<ScSensorInfo> scSensorInfoList = scSensorInfoService.list(wrapper);
        return R.ok(scSensorInfoList,"查询成功");
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/pageList/{pageNum}/{pageSize}")
    public R pageList(@PathVariable("pageNum")Long pageNum,
                  @PathVariable("pageSize")Long pageSize,
                  @RequestBody ScSensorInfo scSensorInfo){
        //属性为null或者"" ,查询全部
        QueryWrapper<ScSensorInfo> wrapper = new QueryWrapper<ScSensorInfo>();
        wrapper.eq(!StringUtil.isNullOrEmpty(String.valueOf(scSensorInfo.getId()) )
                &&  !"null".equals(String.valueOf(scSensorInfo.getId())), "id",scSensorInfo.getId());
        wrapper.eq(!StringUtil.isNullOrEmpty(scSensorInfo.getSensorName()), "sensor_name",scSensorInfo.getSensorName());
        wrapper.eq(!StringUtil.isNullOrEmpty(scSensorInfo.getSensorUnit()), "sensor_unit",scSensorInfo.getSensorUnit());
        wrapper.eq(!StringUtil.isNullOrEmpty(scSensorInfo.getSensorMetric()), "sensor_metric",scSensorInfo.getSensorMetric());
        wrapper.eq(!StringUtil.isNullOrEmpty(scSensorInfo.getSensorCode()), "sensor_code",scSensorInfo.getSensorCode());

        IPage<ScSensorInfo> page = scSensorInfoService.page(
        new Page<>(pageNum, pageSize),
                wrapper);
        return R.ok(page,"分页查询成功");
    }

    @ApiOperation(value = "根据id查询详情")
    @GetMapping("/get/{id}")
    public R get(@PathVariable("id") Long id){
        ScSensorInfo scSensorInfo = scSensorInfoService.getById(id);
        return R.ok(scSensorInfo,"查询成功");
    }

    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public R save(@RequestBody ScSensorInfo scSensorInfo){

        //sensorcode重复校验
        long count = scSensorInfoService.count(Wrappers.<ScSensorInfo>query().lambda().eq(ScSensorInfo::getSensorCode,scSensorInfo.getSensorCode()));

        if(count==0){
            boolean b = scSensorInfoService.save(scSensorInfo);
            if (b){
                return R.ok(b,"添加成功");
            }
            return R.failed(null,"添加失败");
        }else{
            return R.failed(null,"sensorCode已存在");
        }
     }

    @ApiOperation(value = "根据id删除")
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id){
        scSensorInfoService.removeById(id);
        return R.ok(null,"删除成功");
    }

    @ApiOperation(value = "根据id修改")
    @PostMapping("/update/{id}")
    public R update(@RequestBody ScSensorInfo scSensorInfo){
        if(scSensorInfo.getId()!=null)
        {
            List<ScSensorInfo> scSensorInfoList=scSensorInfoService.list(Wrappers.<ScSensorInfo>query().lambda()
                    .ne(ScSensorInfo::getId,scSensorInfo.getId())
                    .eq(ScSensorInfo::getSensorCode,scSensorInfo.getSensorCode()));

            if(scSensorInfoList !=null  &&  scSensorInfoList.size()>0 ) { //判断sensorcode是否存在

                return R.failed(null,"sensorcode已存在");

            }else{
                return R.ok( scSensorInfoService.updateById(scSensorInfo),"操作完成");
            }

        }
        return R.failed(null,"id不能为空");
    }


}
